/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package morenoapp.model.impl;

import morenoapp.model.data.MorenoQuestionFace;
import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import morenoapp.model.data.MorenoQuestionItemFace;
import morenoapp.model.enums.MorenoQuestionType;
import morenoapp.util.MorenoUtil;

/**
 *
 * @author dmitry.mamonov
 */
public class MorenoQuestionImpl extends MorenoModelObject implements Serializable, MorenoQuestionFace {
    private static final long serialVersionUID = 9192047932176418L;

    /**
     * Содержание вопроса.
     */
    private String content = "";

    private MorenoQuestionType type = MorenoQuestionType.Uncategorized;

    /**
     * Цвет для обозначения ответов на вопрос в диаграммах.
     */
    private Color color = Color.GRAY;

    /**
     * Специальный симвло для обозрачения ответов на вопрос в диаграммах.
     */
    private String marker = "";

    /**
     * Список позиций для ответов в вопросе.
     */
    Map<Integer, MorenoQuestionItemFace> questionItemsMap = new TreeMap();

    MorenoQuestionImpl(MorenoModelImpl model) {
        super(model);
    }

    @Override
    public String getContent() {
        return content;
    }

    @Override
    public void setContent(String content) {
        checkNotNull(content);

        String oldValue = this.content;
        this.content = content;
        this.firePropertyChange("content",oldValue,this.content);
        getModel().fireQuestionChanged(this);
    }

    @Override
    public Color getColor() {
        return color;
    }

    @Override
    public void setColor(Color color) {
        checkNotNull(color);

        Color oldColor = this.color;
        this.color = color;
        this.firePropertyChange("color",oldColor,this.color);

        getModel().fireQuestionChanged(this);
    }

    @Override
    public String getMarker() {
        return marker;
    }

    @Override
    public void setMarker(String marker) {
        checkNotNull(marker);

        String oldValue = this.marker;
        this.marker = marker;
        this.firePropertyChange("marker",oldValue,this.marker);
        getModel().fireQuestionChanged(this);
    }

    @Override
    public MorenoQuestionType getType() {
        if (this.type == null) {
            this.type = MorenoQuestionType.Uncategorized;
        }
        return this.type;
    }

    @Override
    public void setType(MorenoQuestionType _type) {
        checkNotNull(_type);

        MorenoQuestionType oldValue = this.type;
        this.type = _type;
        this.firePropertyChange("type",oldValue,this.type);
        getModel().fireQuestionChanged(this);
    }

    @Override
    public int getNumber() {
        return getModel().getQuestionsList().indexOf(this) + 1;
    }

    @Override
    public String getMarkerForQuestionItem(MorenoQuestionItemFace questionItem) {
        return getMarker() + questionItem.getItemNumber();
    }

    @Override
    public MorenoQuestionItemFace getQuestionItemByNumber(int number) {
        if (number < 1 || number > getModel().getRespondentsList().size()) {
            throw new IllegalArgumentException("Illegal number of answer position in question: " + number); //NOI18N
        }
        Integer keyNumber = Integer.valueOf(number);
        if (!questionItemsMap.containsKey(keyNumber)) {
            questionItemsMap.put(keyNumber, new MorenoQuestionItemImpl(this, number));
        }
        return questionItemsMap.get(keyNumber);
    }

    @Override
    public List<MorenoQuestionItemFace> getQuestionItemsList() {
        return new ArrayList<MorenoQuestionItemFace>(questionItemsMap.values());
    }

    @Override
    public MorenoQuestionFace getPrevQuestion() {
        List<MorenoQuestionFace> questionsList = getModel().getQuestionsList();
        int index = questionsList.indexOf(this);
        if (index > 0) {
            return getModel().getQuestionsList().get(index - 1);
        } else {
            return null;
        }
    }

    @Override
    public MorenoQuestionFace getNextQuestion() {
        List<MorenoQuestionFace> questionsList = getModel().getQuestionsList();
        int index = questionsList.indexOf(this);
        if (index + 1 < questionsList.size()) {
            return getModel().getQuestionsList().get(index + 1);
        } else {
            return null;
        }
    }

    //TODO могут быть проблемы при работе с моделью данных
//    @Override
//    public void assign(MorenoQuestionFace question) {
//        this.setContent(question.getContent());
//        this.setColor(question.getColor());
//        this.setMarker(question.getMarker());
//    }

    @Override
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException ex) {
            throw new Error("Error in program", ex); //NOI18N
        }
    }

    @Override
    public String toPlainString() {
        return "" + getNumber() + ". " + getContent(); //NOI18N
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "@" + getContent(); //NOI18N
    }

    @Override
    public String toHtmlString() {
        return MorenoUtil.wrapTextWithColor(color, toPlainString());
    }
}
